" 创建新的 .html 文件时自动调用 InsertHTMLHeader 函数
autocmd BufNewFile *.html call InsertHTMLHeader()
"新建.c,.h,.sh,.java文件，自动插入文件头 
autocmd BufNewFile *.cpp,*.[ch],*.sh,*.rb,*.java,*.py exec ":call SetTitle()" 
" 定义函数 InsertHTMLHeader，用于插入 HTML 文件头
function! InsertHTMLHeader()
    call setline(1, "<!DOCTYPE html>")
    call append(line("."), "<html lang=\"en\">")
    call append(line(".")+1, "<head>")
    call append(line(".")+2, "    <meta charset=\"UTF-8\">")
    call append(line(".")+3, "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">")
    call append(line(".")+4, "    <title>New HTML File</title>")
    call append(line(".")+5, "</head>")
    call append(line(".")+6, "<body>")
    call append(line(".")+7, "")
    call append(line(".")+8, "</body>")
    call append(line(".")+9, "</html>")
    call cursor(7, 1)  " 将光标定位到 <body> 内容的位置
endfunction

""定义函数SetTitle，自动插入文件头 
func SetTitle() 
        "如果文件类型为.sh文件 
        if &filetype == 'sh' 
                call setline(1,"\#!/bin/bash") 
                call append(line("."), "") 
    elseif &filetype == 'python'
        call setline(1,"#!/usr/bin/env python")
        call append(line("."),"# coding=utf-8")
            call append(line(".")+1, "") 

    elseif &filetype == 'ruby'
        call setline(1,"#!/usr/bin/env ruby")
        call append(line("."),"# encoding: utf-8")
            call append(line(".")+1, "")

"    elseif &filetype == 'mkd'
"        call setline(1,"<head><meta charset=\"UTF-8\"></head>")
        else 
                call setline(1, "/*************************************************************************") 
                call append(line("."), "        > File Name: ".expand("%")) 
                call append(line(".")+1, "        > Author: ") 
                call append(line(".")+2, "        > Mail: ") 
                call append(line(".")+3, "        > Created Time: ".strftime("%c")) 
                call append(line(".")+4, " ************************************************************************/") 
                call append(line(".")+5, "")
        endif
        if expand("%:e") == 'cpp'
                call append(line(".")+6, "#include<iostream>")
                call append(line(".")+7, "using namespace std;")
                call append(line(".")+8, "")
        endif
        if &filetype == 'c'
                call append(line(".")+6, "#include<stdio.h>")
                call append(line(".")+7, "")
        endif
        if expand("%:e") == 'h'
                call append(line(".")+6, "#ifndef _".toupper(expand("%:r"))."_H")
                call append(line(".")+7, "#define _".toupper(expand("%:r"))."_H")
                call append(line(".")+8, "#endif")
        endif
        if &filetype == 'java'
                call append(line(".")+6,"public class ".expand("%:r"))
                call append(line(".")+7,"")
        endif
        "新建文件后，自动定位到文件末尾
endfunc 
autocmd BufNewFile * normal G


